//
// Created by steven on 2017/2/23.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMCarViewController.h"
#import "GMAddQRCarViewController.h"
#import "GMCarDetailViewController.h"
#import "SubLBXScanViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>

enum {
    AnnotationViewControllerAnnotationTypeRed = 0,
    AnnotationViewControllerAnnotationTypeGreen,
    AnnotationViewControllerAnnotationTypePurple
};

@interface GMCarViewController()<MAMapViewDelegate>

@property (nonatomic, strong)MAMapView *mapView;
@property (nonatomic, strong) NSMutableArray *annotations;

@end

@implementation GMCarViewController {

}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initAnnotations];

    [self setupMainView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加车辆" style:UIBarButtonItemStylePlain
                                                                       target:self action:@selector(rightBarButtonItemPressed)];
    [self.navigationItem setRightBarButtonItem:rightButtonItem animated:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self.mapView addAnnotations:self.annotations];
    [self.mapView showAnnotations:self.annotations edgePadding:UIEdgeInsetsMake(20, 20, 20, 80) animated:YES];
}

#pragma mark - Initialization
- (void)initAnnotations
{
    self.annotations = [NSMutableArray array];

    CLLocationCoordinate2D coordinates[10] = {
            {39.992520, 116.336170},
            {39.992520, 116.336170},
            {39.998293, 116.352343},
            {40.004087, 116.348904},
            {40.001442, 116.353915},
            {39.989105, 116.353915},
            {39.989098, 116.360200},
            {39.998439, 116.360201},
            {39.979590, 116.324219},
            {39.978234, 116.352792}};

    for (int i = 0; i < 10; ++i)
    {
        MAPointAnnotation *a1 = [[MAPointAnnotation alloc] init];
        a1.coordinate = coordinates[i];
        a1.title      = [NSString stringWithFormat:@"anno: %d", i];
        [self.annotations addObject:a1];
    }
}

#pragma mark - setup main view
- (void)setupMainView {
    self.navigationItem.title = @"客户守卫";

    [self.view addSubview:self.mapView];
    [self.mapView autoPinEdgesToSuperviewEdges];
}

#pragma mark - rightBarButtonItemPressed
- (void)rightBarButtonItemPressed {
//    GMAddQRCarViewController *addCarViewController = [[GMAddQRCarViewController alloc] init];
//    [self.navigationController pushViewController:addCarViewController animated:YES];

    //设置扫码区域参数
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 44;
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
    style.photoframeLineW = 2;
    style.photoframeAngleW = 18;
    style.photoframeAngleH = 18;
    style.isNeedShowRetangle = NO;

    style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;

    style.colorAngle = [UIColor colorWithRed:0./255 green:200./255. blue:20./255. alpha:1.0];


    //qq里面的线条图片
    UIImage *imgLine = [UIImage imageNamed:@"CodeScan.bundle/qrcode_Scan_weixin_Line"];

    // imgLine = [self createImageWithColor:[UIColor colorWithRed:120/255. green:221/255. blue:71/255. alpha:1.0]];

    style.animationImage = imgLine;

    SubLBXScanViewController *subLBXScanViewController = [[SubLBXScanViewController alloc] init];
    subLBXScanViewController.style = style;
    [self.navigationController pushViewController:subLBXScanViewController animated:YES];
}

#pragma mark - map
- (MAMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MAMapView alloc] init];
        _mapView.delegate = self;
    }

    return _mapView;
}

#pragma mark - Map Delegate

/*!
 @brief 根据anntation生成对应的View
 @param mapView 地图View
 @param annotation 指定的标注
 @return 生成的标注View
 */
- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
        }

        annotationView.canShowCallout               = NO;
//        annotationView.animatesDrop                 = YES;
//        annotationView.draggable                    = YES;
//        annotationView.rightCalloutAccessoryView    = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//        annotationView.pinColor                     = 1;
        annotationView.image = [UIImage imageNamed:@"car-guard-icon"];

        return annotationView;
    }

    return nil;
}

/*!
 @brief 当mapView新添加annotation views时调用此接口
 @param mapView 地图View
 @param views 新添加的annotation views
 */
- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views {

}

/*!
 @brief 当选中一个annotation views时调用此接口
 @param mapView 地图View
 @param views 选中的annotation views
 */
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    GMCarDetailViewController *carDetailViewController = [[GMCarDetailViewController alloc] init];
    [self.navigationController pushViewController:carDetailViewController animated:YES];
}

/*!
 @brief 当取消选中一个annotation views时调用此接口
 @param mapView 地图View
 @param views 取消选中的annotation views
 */
- (void)mapView:(MAMapView *)mapView didDeselectAnnotationView:(MAAnnotationView *)view {

}

/*!
 @brief 标注view的accessory view(必须继承自UIControl)被点击时调用此接口
 @param mapView 地图View
 @param annotationView callout所属的标注view
 @param control 对应的control
 */
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

}

/**
 *  标注view的calloutview整体点击时调用此接口
 *
 *  @param mapView 地图的view
 *  @param view calloutView所属的annotationView
 */
- (void)mapView:(MAMapView *)mapView didAnnotationViewCalloutTapped:(MAAnnotationView *)view {

}

/*!
 @brief 拖动annotation view时view的状态变化，ios3.2以后支持
 @param mapView 地图View
 @param view annotation view
 @param newState 新状态
 @param oldState 旧状态
 */
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState fromOldState:(MAAnnotationViewDragState)oldState {

}

@end
